package com.journey.org.ui.knowledge

import android.app.Application
import android.text.TextUtils
import androidx.databinding.ObservableArrayList
import androidx.databinding.ObservableInt
import androidx.lifecycle.viewModelScope
import com.journey.org.BR
import com.journey.org.R
import com.journey.org.app.HttpManager
import com.journey.org.app.requestHttpManager
import com.journey.org.app.requestPlayAndroid
import com.journey.org.entity.PlayAndroidBannerEntity
import com.journey.org.entity.PlayAndroidHomeEntity
import com.journey.org.ui.knowledge.gankio.GankMenuFragment
import com.journey.org.ui.knowledge.play.PlayAndroidMenuFragment
import com.mvvmhabit.org.base.BaseViewModel
import com.mvvmhabit.org.base.MultiItemViewModel
import com.mvvmhabit.org.binding.command.BindingAction
import com.mvvmhabit.org.binding.command.BindingCommand
import com.mvvmhabit.org.custom.LoadingLayout
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.cancel
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import me.tatarka.bindingcollectionadapter2.ItemBinding

/**
 * @Date: 2020/11/17 14:18
 * @Author:小妖
 * @Description: 知识
 */
class KnowLedgeViewModel(application: Application) : BaseViewModel(application) {

    val requestStateField = ObservableInt(LoadingLayout.HIDE_LAYOUT)

    val onClickEnterGankListCommand = BindingCommand<Any>(object : BindingAction {
        override fun call() {
            startContainerActivity(GankMenuFragment::class.qualifiedName, null)
        }
    })

    val onClickEnterPlayListCommand = BindingCommand<Any>(object : BindingAction {
        override fun call() {
            startContainerActivity(PlayAndroidMenuFragment::class.qualifiedName, null)
        }
    })

    val onClickAgainLoadCommand = BindingCommand<Any>(object : BindingAction {
        override fun call() {
            page = 0
            loadPlayAndroidBanner()
        }
    })
    val onClickLoadMoreCommand = BindingCommand<Any>(object : BindingAction {
        override fun call() {
            ++page
            loadPlayAndroidBanner()
        }
    })

    val onClickRefreshCommand = BindingCommand<Any>(object : BindingAction {
        override fun call() {
            page = 0
            loadPlayAndroidBanner()
        }
    })

    /**************************************** banner ****************************************/
    val bannerItemBinding =
        ItemBinding.of<KnowLedgeBannerItemViewModel>(BR.viewModel, R.layout.knowledge_banner_item)

    val bannerItems = ObservableArrayList<KnowLedgeBannerItemViewModel>()

    fun loadPlayAndroidBanner() {
        bannerItems.clear()
        viewModelScope.launch(Dispatchers.Main) {
            if (page == 0) {
                showLoadDialog()
            }
            loadPlayAndroidHomeList()
            requestPlayAndroid<List<PlayAndroidBannerEntity>> {
                onLoader {
                    HttpManager.getInstance().loadPlayAndroidBannerList()!!
                }
                onSuccess {
                    if (it.isNotEmpty()) {
                        it.forEach { item ->
                            bannerItems.add(
                                KnowLedgeBannerItemViewModel(
                                    this@KnowLedgeViewModel,
                                    item
                                )
                            )
                        }
                    }
                }
                onError {
                    dismissLoadDialog()
                }
            }
        }
    }

    /************************************** items ****************************************/
    var page = 0

    val itemBinding =
        ItemBinding.of<MultiItemViewModel<KnowLedgeViewModel>> { itemBinding, _, item ->
            when (item.getItemType().toString()) {
                "menu" -> {
                    itemBinding.set(BR.viewModel, R.layout.knowledge_menu_item)
                }
                "blog" -> {
                    itemBinding.set(BR.viewModel, R.layout.knowledge_blog_item)
                }
                "body" -> {
                    itemBinding.set(BR.viewModel, R.layout.knowledge_body_item)
                }
            }
        }

    val items = ObservableArrayList<MultiItemViewModel<KnowLedgeViewModel>>()

    private fun loadPlayAndroidHomeList() {
        if (page == 0) {
            items.clear()
            loadHomeMenuData()
            loadMineBlogData()
        }
        requestPlayAndroid<PlayAndroidHomeEntity> {
            requestHttpManager {
                onLoader {
                    HttpManager.getInstance().loadPlayAndroidHomeList(page)!!
                }
                onSuccess {
                    it.datas.forEach { entity ->
                        if (TextUtils.isEmpty(entity.envelopePic)) {
                            val blogViewModel =
                                KnowLedgeBlogViewModel(this@KnowLedgeViewModel, entity)
                            blogViewModel.multiItemType("blog")
                            items.add(blogViewModel)
                        } else {
                            val bodyViewModel =
                                KnowLedgeBodyViewModel(this@KnowLedgeViewModel, entity)
                            bodyViewModel.multiItemType("body")
                            items.add(bodyViewModel)
                        }
                    }
                }
                onComplete {
//                    requestStateField.set(LoadingLayout.HIDE_LAYOUT)
                    dismissLoadDialog()
                }
            }
        }
    }

    // 加载自己需要的博客地址
    private fun loadMineBlogData() {

    }

    // 加载菜单数据
    private fun loadHomeMenuData() {
        val menuViewModel = KnowLedgeMenuViewModel(this)
        menuViewModel.multiItemType("menu")
        items.add(menuViewModel)
    }
}